<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleAction">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <template v-if="!nodeData.root">
                <el-dropdown-item command="edit">编辑部分</el-dropdown-item>
                <el-dropdown-item command="del">删除部门</el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { reqDelDepartments } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      type: Object,
      required: true
    }
  },
  methods: {
    async handleAction(type) {
      if (type === 'add') {
        // 这个是把子部门的id传给父级,将id和类型都传给父组件，父组件来进行判断
        // 加type标志位的目的是区分添加和编辑
        this.$emit('on-open', { id: this.nodeData.id, type: 'add' })
        // 添加子部门
      } else if (type === 'edit') {
        // 编辑子部门
        // 点击后:打开编辑的弹窗
        this.$emit('on-open', { id: this.nodeData.id, type: 'edit' })
      } else if (type === 'del') {
        // 删除子部门
        // 进行提示
        const ret = await this.$confirm('确认要删除部门吗？, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        // 判断点击的是确认还是取消
        if (ret === 'cancel') return

        try {
          // 调删除的接口
          const delRet = await reqDelDepartments(this.nodeData.id)
          if (delRet.code === 10000) {
            this.$emit('action-success')
          } else {
            this.$message.error('删除部门失败')
          }
        } catch {
          this.$message.error('删除部门失败')
        }
      }
    }
  }
}
</script>
